package com.example.demo.controller;

import java.util.List;

import com.example.demo.entity.Banner;
import com.example.demo.service.IBannerService;
import com.example.demo.utils.PageResult;
import com.example.demo.utils.PassToken;
import com.example.demo.utils.ServiceResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


/**
 * bannerController
 * 
 * @author ruoyi
 * @date 2025-03-27
 */
@RestController
@RequestMapping("/system/banner")
public class BannerController {
    @Autowired
    private IBannerService bannerService;

    /**
     * 查询banner列表
     */
    @PassToken
    @GetMapping("/list")
    public ServiceResponse list(Banner banner, @RequestParam(defaultValue = "1")int pageNum,@RequestParam(defaultValue = "3")int pageSize) {
//        startPage();
        //分页
        PageResult pageResult = bannerService.selectBannerList(banner,pageNum,pageSize);
        return ServiceResponse.createBySuccess(200, pageResult, "查询成功");
    }

//    /**
//     * 导出banner列表
//     */
//    @PreAuthorize("@ss.hasPermi('system:banner:export')")
//    @Log(title = "banner", businessType = BusinessType.EXPORT)
//    @PostMapping("/export")
//    public void export(HttpServletResponse response, Banner banner)
//    {
//        List<Banner> list = bannerService.selectBannerList(banner);
//        ExcelUtil<Banner> util = new ExcelUtil<Banner>(Banner.class);
//        util.exportExcel(response, list, "banner数据");
//    }

    /**
     * 获取banner详细信息
     */
    @GetMapping(value = "/{id}")
    public ServiceResponse getInfo(@PathVariable("id") Long id)
    {
        return ServiceResponse.createBySuccess(200,bannerService.selectBannerById(id),"查询成功");
    }

    /**
     * 新增banner
     */
    @PostMapping
    public ServiceResponse<? extends Object> add (@RequestBody Banner banner) {
        int i = bannerService.insertBanner(banner);
        if (i > 0) {
            return ServiceResponse.createBySuccess(200, "", "添加成功");
        } else {
            return ServiceResponse.createByFail(500, "添加失败");
        }
    }

    /**
     * 修改banner
     */

    @PutMapping
    public ServiceResponse edit(@RequestBody Banner banner) {
        int i = bannerService.updateBanner(banner);
        if (i > 0) {
            return ServiceResponse.createBySuccess(200, " ", "更新成功");
        } else {
            return ServiceResponse.createByFail(500, "更新失败");
        }
    }

    /**
     * 删除banner
     */
    @DeleteMapping("/{ids}")
    public ServiceResponse remove(@PathVariable Long[] ids) {
        int i = bannerService.deleteBannerByIds(ids);
        if (i > 0) {
            return ServiceResponse.createBySuccess(200, " ", "删除成功");
        } else {
            return ServiceResponse.createByFail(500, "删除失败");
        }
    }
}
